<template>
  <div class="Roles-container">
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getOrderList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table
        :data="orderlist"
        border
        stripe
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column
          prop="order_number"
          label="订单编号"
        >
        </el-table-column>
        <el-table-column
          prop="order_price"
          label="订单单价"
        >
        </el-table-column>
        <el-table-column
          prop="pay_status"
          label="是否付款"
        >
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.pay_status==='1'"
              type="success"
            >已付款</el-tag>
            <el-tag
              v-else
              type="danger"
            >未付款</el-tag>

          </template>
        </el-table-column>
        <el-table-column
          prop="is_send"
          label="是否发货"
        >
        </el-table-column>
        <el-table-column
          prop="create_time"
          label="下单时间"
        ><template slot-scope="scope">
            {{ scope.row.create_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 文字提示 -->
            <el-tooltip
              effect="dark"
              content="修改地址"
              placement="top-start"
              :enterable="false"
            >
              <!-- 修改按钮 -->
              <el-button
                type="primary"
                icon="el-icon-edit"
                v-model="scope.row.order_id"
                size="mini"
                @click="showBox"
              ></el-button>
            </el-tooltip>
            <!-- 文字提示 -->
            <el-tooltip
              effect="dark"
              content="物流进度"
              placement="top-start"
              :enterable="false"
            >
              <!-- 按钮 -->
              <el-button
                type="success"
                icon="el-icon-location"
                size="mini"
                @click="showProgressBox"
              ></el-button>

            </el-tooltip>
            <!--  -->
            <el-tooltip
              effect="dark"
              content="修改状态"
              placement="top-start"
              :enterable="false"
            >
              <el-button
                type="warning"
                icon="el-icon-star-off"
                size="mini"
                @click="editStatus(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>

      </el-table>
      <!-- 分页区域 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5,10,15]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressVisible"
      width="50%"
      @close="addressDialogClosed"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormref"
        label-width="100px"
      >
        <el-form-item
          label="省市区/县"
          prop="address1"
        >
          <el-cascader
            :options="cityData"
            v-model="addressForm.address1"
            @change="show"
          ></el-cascader>
        </el-form-item>
        <el-form-item
          label="详细地址"
          prop="address2"
        >
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 物流信息对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="progressVisible"
      width="50%"
    >
    </el-dialog>
    <!-- 修改订单状态对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="statusVisible"
      width="50%"
    >
      <el-form
        :model="statusInfo"
        :rules="editFormrules"
        ref="editFormRef"
        label-width="100px"
      >
        <el-form-item
          label="订单是否发货"
          prop="is_send"
        >
          <el-input v-model="statusInfo.is_send"></el-input>

        </el-form-item>
        <el-form-item
          label="订单支付"
          prop="order_pay"
        >
          <el-input v-model="statusInfo.order_pay"></el-input>

        </el-form-item>
        <el-form-item
          label="订单价格"
          prop="order_price"
        >
          <el-input v-model="statusInfo.order_price"></el-input>

        </el-form-item>
        <el-form-item
          label="订单数量"
          prop="order_number"
        >
          <el-input v-model="statusInfo.order_number"></el-input>

        </el-form-item>
        <el-form-item
          label="支付状态"
          prop="pay_status"
        >
          <el-input v-model="statusInfo.pay_status"></el-input>

        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="statusVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="edit(statusInfo)"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from "./citydata.js"
export default {
  data() {
    return {
      // 订单列表数据
      orderlist: [],
      // 查询条件
      queryInfo: {
        query: "",
        // 当前页码值
        pagenum: 1,
        // 每页数据显示多少条
        pagesize: 10
      },
      // 总数与条数
      total: 0,
      //控制修改地址对话框的显示与隐藏
      addressVisible: false,
      // 修改地址表单对象
      addressForm: {
        address1: [],
        address2: ""
      },
      // 验证规则
      addressFormRules: {
        address1: [
          { required: true, message: "请选择省市区/县", tirgger: "blur" }
        ],
        address2: [
          { required: true, message: "请填写详细地址", tirgger: "blur" }
        ]
      },
      cityData,
      // 控制物流信息对话框的显示与隐藏
      progressVisible: false,
      // 控制订单状态对话框的显示与隐藏
      statusVisible: false,
      // 修改订单状态表单对象

      //
      editFormrules: {},

      // 修改状态对象
      statusInfo: {}
    }
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get("orders", {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$Message.error("获取订单数据失败")
      }
      this.orderlist = res.data.goods
      this.total = res.data.total
      console.log(this.orderlist)
      console.log(this.total)
    },
    // 监听pagesize 改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 监听页码值改变的事件
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 展示修改地址的对话框
    showBox() {
      this.addressVisible = true
    },
    // 修改对话框关闭事件
    addressDialogClosed() {
      this.$refs.addressFormref.resetFields()
    },
    // 展示物流信息对话框
    async showProgressBox() {
      // const { data: res } = await this.$http.get(`/kuaidi/JD0074160621286`)
      // if (res.meta.status !== 200) {
      //   return this.$Message.error("获取物流信息失败")
      // }
      // this.$Message.success("获取物流信息成功")
      this.progressVisible = true
    },
    // 级联选择器发送变化事件
    show() {
      this.addressForm.address1 = this.addressForm.address1.join(",")
      console.log(this.addressForm.address1)
    },
    editStatus(statusInfo) {
      console.log(statusInfo)
      this.statusInfo = statusInfo

      this.statusVisible = true
    },
    async edit(statusInfo) {
      const { data: res } = await this.$http.put(
        `orders/${statusInfo.order_id}`,
        this.statusInfo
      )
      if (res.meta.status !== 201) {
        return this.$Message.error("修改失败")
      }

      this.$Message.success("成功")
      console.log(this.orderlist)
      this.statusVisible = false
    }
  },
  created() {
    this.getOrderList()
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
